/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package mobileipsimulation;

import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.image.*;
import javafx.scene.control.TextBox;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import javafx.scene.paint.Color;

import messages.MessageSender;

import javafx.scene.input.MouseEvent;






var inHome = true;
var inForeign = false;

var whereAmI = -1;
var okYouMayGoNow = false;

var width:Number = bind scene.width on replace {
    mobileNode.translateX = scene.width/2;
    mobileNode.translateY = scene.height-150;
};
var agent;
var height:Number = bind scene.height;
var sceneHeight = 700;
var sceneWidth = 1150;

var mobAg1X = 300;
var mobAg1Y = 250;
var mobAg1Rad = 350;

var mobAg2X = sceneWidth-300;
var mobAg2Y = 250;
var mobAg2Rad = 350;

/*Mobile Node 1 parameters*/
var nodeip = "localhost";
var home = "localhost";
var mac = "00-00-00-00-00-00-00-E0";
/*Mobile Node 2 parameters*/
var nodeip2 = "localhost2";
var home2 = "localhost";
var mac2 = "00-00-00-00-00-00-00-E0";
/*Home Agent parameters*/
var homeip = "localhost";
var homeflag = "home";
var homeport = 49552;
/*Foreign Agent parameters*/
var foreignip = "localhost";
//var foreignip = "192.111.0.1";
var foreignflag = "foreign";
var foreignport = 49553;

 // def textbox: JEditorPane = new JEditorPane("text/plain","");
 // textbox.setBackground(new java.awt.Color(0,0,0,0));
 // def sbox = SwingComponent.wrap( textbox );

var strangeText = Text {
        effect: DropShadow {}
        font: bind Font.font("Serif", 20)
        fill: Color.color(1,1,1);
       x: bind scene.width/2
       y: bind scene.height/2
        
       /* content:    "Bk is smart\n"
                    "But Olga is smarter\n"
                    "WTF i'm writing here\n"
                    "It's just a stupid test\n"
                    "But it's pretty beautiful\n"
                    "Here will appear the msgs U're trying to send!\n"
                    "--------------------------------\n"*/
        content: "LOG\n"
      }

var text = TextBox {
        translateX: sceneWidth/2
        translateY: 50
        text: ""
        columns: 20
        selectOnFocus: true
        editable: false
}

var homeRadius = AgentRadius {
        centX: mobAg1X
        centY: mobAg1Y
        rad: mobAg1Rad
        alpha: 0.5
};

var homeAgent = MobilityAgentImage {
      centerX: mobAg1X
      centerY: mobAg1Y
      img: "home_agent.png"
      ip:homeip
      flag:homeflag
      port:homeport
     // msgHA:"1"
      home:true      
};
var homeAgentText = TextBox {
        text: bind foreignAgent.msgHA;
        columns: 30
        selectOnFocus: true
        editable: false
        translateX: bind homeAgent.tx;
        translateY: bind homeAgent.ty;
}

var foreignAgent = MobilityAgentImage {
   centerX: mobAg2X
   centerY: mobAg2Y
   img: "foreign_agent.png"
   ip:foreignip
   flag:foreignflag
   port:foreignport
   home:false
};
var foreignAgentText = TextBox {
        text: bind foreignAgent.msgFA;
        columns: 30
        selectOnFocus: true
        editable: false
        translateX: bind foreignAgent.tx;
        translateY: bind foreignAgent.ty;
}
var foreignRadius = AgentRadius {
        centX: mobAg2X
        centY: mobAg2Y
        rad: mobAg2Rad
        alpha: 0.2
};
var mobileNode = DraggableImage {
    maxX: bind width
    maxY: bind height
    image: Image { url: "{__DIR__}images/htc-touch-diamond.png" }
    ip:nodeip
    homeip:home
    mac:mac
    //onMouseDragged: text.s
};
var mobileNodeText = TextBox {
        //text: "Hey! I am the mobile node's textbox ehehe:D"
       translateX: bind mobileNode.tx - 100
        translateY: bind mobileNode.ty - 20
        text: bind mobileNode.msg
        columns: 30
        selectOnFocus: true
        editable: false
        
}
var ipTextBox = TextBox {
        translateX: 5
        translateY: sceneHeight - 80
        text: "ip to send to"
        columns: 20
        selectOnFocus: true
        editable: true
}
var messageTextBox = TextBox {
        translateX: 5
        translateY: sceneHeight - 55
        text: "message to send"
        columns: 20
        selectOnFocus: true
        editable: true
}
var but = Button {
        text: "Send Message"
        translateX: 15
        translateY: sceneHeight - 25
        action: function() {
                var textBoxIp = ipTextBox.text;
                var textBoxMsg = messageTextBox.text;
              //  var ms = new MessageSender(textBoxIp,textBoxMsg,2345);
                var ms = new MessageSender(textBoxIp,textBoxMsg,homeAgent.port);
                //strangeText.content = "{strangeText.content} The message is: [{messageTextBox.text}]\n";
                //agent = new Agent("dsf");
                //var ag = AgentRadius{};
                //Agent.print(ag);
                if(inHome) {
                    text.text = "Sending message at home.."
                }else

                if(inForeign){
                    text.text = "Sending message at foreign.."
                }
        }
}


var scene:Scene = Scene {      
   width: sceneWidth;
   height: sceneHeight;
    content: Group {
       /* onMouseClicked: function(e:MouseEvent):Void{
             strangeText.content = "[homeAgent: {homeAgent.msg}]\n"
                                            "[foreignAgent: {foreignAgent.msg}]\n"
                                            "[MobileNode: {mobileNode.msg}]\n";
        }*/

        onMouseDragged: function( e : MouseEvent ): Void {
               // java.lang.System.out.println("ON MOUSE DRAGGED: {homeAgent.msgHA}");
              //  homeAgentText.text = homeAgent.msgHA;
           if(foreignRadius.hover and  homeRadius.hover){
                  if(whereAmI != 3){
                        println("DENTRO DAS DUAS");
                        whereAmI = 3;
                   }
            }else if(homeRadius.hover) {
              
                  //  homeAgentText.text = homeAgent.msg;
                  //  foreignAgentText.text = foreignAgent.msg;
                  //  mobileNodeText.text = mobileNode.msg;


                  //  strangeText.
                    
                    if(whereAmI != 0){
                        println("in the home!!!!");
                        mobileNode.node.outOfRangeFA();
                        whereAmI = 0;
                        inHome = true;
                        inForeign = false;
                        text.text = "MN at home!";
                        mobileNode.node.setHome(true);
                        mobileNode.node.setHasSignal(true); 
                    }
                    
             }else if(foreignRadius.hover) {
                    if(whereAmI != 1){
                         println("in the foreign!!!!");
                          mobileNode.node.outOfRangeHA();
                        inHome = false;
                        inForeign = true;
                        text.text = "MN at the foreign network!";
                        mobileNode.node.setHome(false);
                        mobileNode.node.setHasSignal(true);whereAmI = 1;
                        
                    }
                   
            }else{
                if(whereAmI != 2){
                        if(okYouMayGoNow){
                       mobileNode.node.outOfRangeAll();
                    }
                    println("SEM REDE!!!!");
                    inHome = false;
                    inForeign = false;
                    text.text = "MN NO SIGNAL!";
                    mobileNode.node.setRegisteredAgent("");
                    mobileNode.node.setHome(false);
                    mobileNode.node.setHasSignal(false);
                    whereAmI = 2;
                    okYouMayGoNow = true;
                }
                    
            }
            // println("DRAGGG....");
        }
        content: [
           homeAgent,foreignAgent, homeRadius, foreignRadius,
           mobileNode,but, text, messageTextBox,mobileNodeText,homeAgentText,foreignAgentText
        ]
    }
   

    fill: LinearGradient {
        startX: 0 startY: 0
        endX: 0 endY: 1
        proportional: true
        stops: [
            Stop { offset: 0.0 color: Color.rgb(206, 228, 228) },
            Stop { offset: 1.0 color: Color.rgb(226, 248, 248) },
        ]
    }
};

Stage {
    title: "Simulate MobileIP"
    scene: scene
}
